<?php 
/**
 * 上传头像
 */
class UploadAvater extends APIBase {
	
	public $tag="UploadAvater";
	
	public function before() {
		return true;
	}
	
	public function logic() {
		if (!isset($this->param['suffix']) || ($this->param['suffix']) == "") {
			$suffix="jpg";
		} else {
			$suffix=$this->param['suffix'];
		}
		$res = file_get_contents("php://input") ? file_get_contents("php://input") : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);
		if (!empty($res)) {
			$filename = $this->uid . '_' . time() . '.' . $suffix;
			$pathfile = Game::$avater_sys_path . $filename;
			$path=$_SERVER['DOCUMENT_ROOT'] . Game::$avater_websvr_path;
			$file=$path . $filename;
			$handle=fopen($file,"wb");
			if ($handle) {
				fwrite($handle,$res);
				fclose($handle);				
				$url = "http://" . $_SERVER["SERVER_ADDR"] . ":" . $file;
				$this->data_redis->hset("hu:{$this->uid}",'avater_url',$url);
				$this->returnData(array('avater_url' =>$url));
			} else {
				$this->returnError(301,"create photo failed!");
			}
		} else {
			$this->returnError(302,"photo is empty!");
		}		
	}
	
	public function after() {
		$this->deinitDataRedis();
	}
		
}

?>
